package com.qf.test.test.servler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.test.test.mapper.UserMapper;
import com.qf.test.test.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    //会报错  无法注入 因为request respone session model是springmvc默认支持的对象参数
    //是子容器的对象  而这里service属于spring父容器管理的对象 无法使用子容器中的对象
    //@Autowired
    //private HttpServletRequest request;

    @Override
    public List<User> findUserAll() {
        //调用dao层
        List<User> list = userMapper.selectList(null);
        return list;
    }

    @Override
    public List<User> findByNameAndSexAndBirthday() {
        //1.创建查询条件构造器
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        //2设置根据名字进行模糊查询
        queryWrapper.like("name","张三");
        //3.设置根据性别进行精准查询条件
        queryWrapper.eq("sex","男");
        //4设置根据日期范围查询条件
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy--MM-dd HH:mm:ss");
        try {
            Date date = sdf.parse("2021-02-01 00:00:00");
            queryWrapper.ge("birthday",date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //5调用dao查询并返回结果
        List<User> list = userMapper.selectList(queryWrapper);
        //6.返回list
        return list;
    }
}
